/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/

/**
	Utilities for string manipulations
	@module jgrouse.string
*/

jgrouse.module(
{
	name : 'jgrouse.string',
	body : function(embed)
	{
		eval(embed);

		/**
			Utilities for string manipulations
			@namespace jgrouse.string
		*/
		jgrouse.string = 
		{
			/**
				Convert the first letter of string to uppercase
				@function {String} upperFirst
				@param {String} str - source string
				@return string with first character converted to upper case
			*/
			upperFirst : function(str)
			{
				if (!str)
				{
					return str;
				}
				return str.substr(0, 1).toUpperCase() + str.substr(1);
			},
			
			/**
				Convert the first letter of string to lower case
				@function {String} lowerFirst
				@param {String} str - source string
				@return string with first character converted to lower case
			*/
			lowerFirst : function(str)
			{
				if (!str)
				{
					return str;
				}
				return str.substr(0, 1).toLowerCase() + str.substr(1);
			},
			
			/**
				Converts string to "mixed case" by removing all '-' signs from string and capitalizing first letters of each word.
				For example, string 'this-is-a-sample' would be converted to 'thisIsASample'
				@function {String} camelize
				@param {String} str - source string
				@return string converted to 'mixed case'
				 
			*/
			camelize : function(str)
			{
				if (!str)
				{
					return str;
				}
				var arr = str.split('-');
				arr[0] = jgrouse.string.lowerFirst(arr[0]);
				for (var i = arr.length - 1; i >= 1; i--)
				{
					arr[i] = jgrouse.string.upperFirst(arr[i]);
				}
				return arr.join('');
			},
			
			/**
				Remove leading whitespaces from string.
				Whitespaces are ' ', '\f', '\n', '\r' and '\t'.
				@function {String} ltrim
				@param {String} str - original string
			*/
			ltrim : function(str)
			{
				for (var i = 0; i < str.length; i++)
				{
					var c = str.charAt(i);
					if (!(c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t'))
					{
						return str.substring(i);
					}
				}
				return '';
			},
			
			/**
				Remove trailing whitespaces from string.
				Whitespaces are ' ', '\f', '\n', '\r' and '\t'.
				@function {String} rtrim
				@param {String} str - original string
			*/
			rtrim : function(str)
			{
				for (var i = str.length - 1; i >= 0; i--)
				{
					var c = str.charAt(i);
					if (!(c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ))
					{
						return str.substring(0, i + 1);
					}
				}
				return '';	
			},
			
			/**
				Remove leading and trailing whitespaces from string.
				Whitespaces are ' ', '\f', '\n', '\r' and '\t'.
				@function {String} trim
				@param {String} str - original string
			*/
			trim : function(str)
			{
				return jgrouse.string.rtrim(jgrouse.string.ltrim(str));
			}
			
		}
	}
})

